package com.robusta.cityuate.services;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.robusta.cityuate.objects.User;
import com.robusta.cityuate.utils.ConnectionUtils;

public class RateUserService extends HTTPService {

	private static final String REQUEST_URL = "/users/";
	private static final String ACTION_URL = "/rate";
	private static final String TAG = RateUserService.class.getName();
	
	private Context mContext=this;
	
	@Override
	protected void onResponse() {
		Intent editUIUpdate = new Intent(assignedFilter);
		switch(responseStatusCode){
		case HttpStatus.SC_NO_CONTENT:
		{// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, true, assignedFilter,
					null);
			// update UI if active
			editUIUpdate.putExtra(RESUlT, true);
			
			
			break;
		}
		case HttpStatus.SC_UNAUTHORIZED:
		case HttpStatus.SC_UNPROCESSABLE_ENTITY:
		{
			// extract the message
			JSONObject responseMsg;
			String msg = "";
			try {
				responseMsg = new JSONObject(responseString);
				for (@SuppressWarnings("unchecked")
				Iterator<String> iterator = responseMsg.keys(); iterator
						.hasNext();) {
					String key = (String) iterator.next();
					msg += responseMsg.getString(key) + "\n";

				}
				Log.e(TAG, "msg " + msg);
			} catch (JSONException e) {
				e.printStackTrace();

			}
			// remove connection from shared pref
			ConnectionUtils.setDisconnecting(mContext, false, assignedFilter,
					msg);
			// update UI if active
			editUIUpdate.putExtra(RESUlT, false);
			Log.d(TAG, "Edit Failed " + msg);
		}
		}
		sendOrderedBroadcast(editUIUpdate, null);
	}

	@Override
	protected void onNoResponse(String msg){
		// remove connection from shared pref keeping result and reason
	ConnectionUtils.setDisconnecting(mContext, false, assignedFilter,
			msg);
	// update UI if active
	Intent loginUIupdate = new Intent(assignedFilter);
	loginUIupdate.putExtra(RESUlT, false);
	sendOrderedBroadcast(loginUIupdate, null);

	}

	@Override
	protected HttpRequestBase initRequest() {
		HttpPost req= new HttpPost(API_URL+REQUEST_URL+extras.getLong(User.USER_ID)+ACTION_URL);
		try {
			req.setEntity(createPostBody());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return req;
	}
	
	
	/**
	 * creates the body of the request from parameters
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private ByteArrayEntity createPostBody()
			throws UnsupportedEncodingException {
		JSONObject usr = new JSONObject();
		// Creating Body
		JSONObject value=new JSONObject();
		
		try {
			value.put(User.USER_RATING_VALUE, extras.getInt(User.USER_RATING_VALUE,0));
			usr.put(User.USER_RATING, value);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Log.e(TAG, usr.toString());
		return new ByteArrayEntity(usr.toString().getBytes("UTF-8"));
	}

}
